<template>
    <div class="snow-page">
        <div class="snow-inner-page">
            <a-form :model="formData.form" :label-col-props="{ span: 5 }" :wrapper-col-props="{ span: 18 }"
                ref="formRef">
                <a-row :gutter="16">
                    <a-col :span="6">
                        <a-form-item field="hazard_name" label="名称">
                            <a-input v-model="formData.form.hazard_name" placeholder="请输入名称" allow-clear />
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-space>
                            <a-button type="primary" @click="onSearch">
                                <template #icon>
                                    <icon-search />
                                </template>
                                <template #default>查询</template>
                            </a-button>
                            <a-button @click="onReset">
                                <template #icon>
                                    <icon-refresh />
                                </template>
                                <template #default>重置</template>
                            </a-button>
                            <a-button type="primary" @click="onAdd">
                                <template #icon>
                                    <icon-PlusOutlined />
                                </template>
                                <template #default>新增</template>
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
            <a-table :data="tableData" row-key="id" size="small" bordered :pagination="false">
                <template #columns>
                    <a-table-column title="危险源编码" :width="140">
                        <template #cell="{ record }">
                            {{ record.hazard_code }}
                        </template>
                    </a-table-column>
                    
                    <a-table-column title="企业">
                        <template #cell="{ record }">
                            {{ formatqiye(record.company_code) }}
                        </template>
                    </a-table-column>
                    <a-table-column title="危险源名称" :width="140">
                        <template #cell="{ record }">
                            {{ record.hazard_name }}
                        </template>
                    </a-table-column>
                    <a-table-column title="危险源等级" :width="140">
                        <template #cell="{ record }">
                            {{ format597(record.hazard_rank) }}
                        </template>
                    </a-table-column>
                    <a-table-column title="R值" :width="120">
                        <template #cell="{ record }">
                            {{ record.rvalue }}
                        </template>
                    </a-table-column>
                    <a-table-column title="地址">
                        <template #cell="{ record }">
                            {{ record.address }}
                        </template>
                    </a-table-column>

                    <a-table-column title="操作" align="center">
                        <template #cell="{ record }">
                            <a-space>
                                <a-button size="mini" type="primary" @click="onEdit(record)">修改</a-button>
                                <a-button size="mini" type="primary" @click="clickName(record)">详情</a-button>
                                <a-button size="mini" type="primary" status="danger"
                                    @click="onDelete2(record)">删除</a-button>
                            </a-space>
                        </template>
                    </a-table-column>
                </template>
            </a-table>
            <a-pagination :total="total" style="margin-top:10px;" @change="changePage" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { Modal, Message } from "@arco-design/web-vue";
import { getweixianListAPI, deleteweixianAPI } from "@/api/modules/weixian/index";
import { getDictBycodeAPI } from "@/api/modules/dictionary/index";
import { getAllqiyeAPI } from "@/api/modules/qiye/index";
import { useUserInfoStore } from "@/store/modules/user-info";
import { onMounted } from "vue";
const router = useRouter();
onMounted(() => {
    getAllqiyeAPIfun();
    getDictBycodeAPIfun();
    init();
});

const optionsqiyeList = ref([]);
async function getAllqiyeAPIfun() {
    let obj = {};
    let res = await getAllqiyeAPI(obj);
    if (res.status == 2) {
        optionsqiyeList.value = res.data;
    }
}

function formatqiye(val){
  for (let i = 0; i < optionsqiyeList.value.length; i++) {
   if(optionsqiyeList.value[i].cq_company_code==val){
    return optionsqiyeList.value[i].name
   }
    
  }
}




const options597 = ref([]);
async function getDictBycodeAPIfun() {
    let res = await getDictBycodeAPI(597);
    if (res.status == 2) {
        options597.value = res.data;
    }
}
function format597(val) {
    for (let i = 0; i < options597.value.length; i++) {
        if (options597.value[i].id == val) {
            return options597.value[i].name
        }

    }
}
async function init() {
    let res = await getweixianListAPI(formData.paged, formData.form);
    total.value = 0
    tableData.value = [];
    if (res.status == 2) {
        total.value = res.data.totalCount;
        tableData.value = res.data.data;
    }
}

const total = ref(0)

const tableData = ref([]);
const formData = reactive({
    form: {
        hazard_name: ""
    },
    paged: 1
});

const formRef = ref();
const onReset = () => {
    formRef.value.resetFields();
    init();
};

function changePage(page) {

    formData.paged = page;
    init()
}

function onSearch() {
    init();
}

function clickName(val) {
    let stores = useUserInfoStore();
    stores.setSharedObject(val);
    router.push({ path: "/qiye/weixianDetails" });
}

function onAdd() {
    let stores = useUserInfoStore();
    stores.removeSharedObject();
    router.push({ path: "/qiye/weixianAdd" });
}

const onEdit = (row: any) => {
    let stores = useUserInfoStore();
    stores.setSharedObject(row);
    router.push({ path: "/qiye/weixianAdd" });
};

const onDelete2 = (row: any) => {
    Modal.warning({
        title: "提示",
        content: "确定删除？",
        hideCancel: false,
        closable: true,
        onOk: async () => {
            try {
                let res = (await deleteweixianAPI(row.id)) as any;
                if (res.status == 2) {
                    Message.success("删除成功");
                } else {
                    Message.error(res.statusMsg);
                }
                init();
            } catch { }
        }
    });
};
</script>

<style lang="scss">
.arco-modal-body {
    text-align: center !important;
}

::v-deep .arco-table-size-small .arco-table-thead .arco-table-cell {
    padding: 2px 16px !important;
    text-align: center !important;
}
</style>